package net.seehope.foodie.code;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import net.seehope.foodie.properties.ProjectConstant;
import net.seehope.foodie.properties.ProjectProperties;


@RestController
public class ValidateCodeController {
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private HttpServletResponse response;
	
	//Spring会去容器中寻找所有ValidateCodeProcessor的子实现，把其bean的名字作为key，bean本身作为value，放到这个map里面
	//这里就是spring在容器中搜集所有ValidateCodeProcessor的子实现
	@Autowired
	private Map<String, ValidateCodeProcessor> validateCodeProcessors;
	
	
	
	//验证码的生成
	//将该方法响应出去
//	@GetMapping(value = {ProjectConstant.IMAGE_CODE_GENERATOR_URL})
//	public void createImageValidateCode() throws IOException {
//
//		ImaValidateCode validateCode = (ImaValidateCode) imageCodeGenerator.generator();
//
//		sessionStrategy.setAttribute(new ServletWebRequest(request, response), ProjectConstant.IMAGE_CODE_IN_SESSION, validateCode);
//	
//		ImageIO.write(validateCode.getImage(), "JPEG", response.getOutputStream());
//		
//	}
//	
//	@GetMapping(value = {ProjectConstant.SMS_CODE_GENERATOR_URL})
//	public void createSmsValidateCode(String mobile) throws IOException {
//		
//		ValidateCode validateCode = new ValidateCode(RandomStringUtils.randomNumeric(4),properties.getCode().getExpireln());
//		
//		sessionStrategy.setAttribute(new ServletWebRequest(request, response), ProjectConstant.IMAGE_CODE_IN_SESSION, validateCode);
//		
//		sender.send(validateCode, mobile);
//		
//		
//	}
	
	//里面的逻辑交给processer去做
	@GetMapping(value = {ProjectConstant.VALIDATE_CODE_GENERATOR_URL+"/{type}"})
	public void createValidateCode(@PathVariable String type) throws Exception {
		//根据前端传进来参数的不同，决定调用不同的create()
		//如此一来，Controller的作用从之前的生成逻辑变成了转发逻辑
		
		validateCodeProcessors.get(type+"ValidateCodeProcessor").create(new ServletWebRequest(request, response));
		
		
	}
	
	

}
